package com.recSys.offlineTask;

import com.recSys.inter.CollectionMapper;
import com.recSys.inter.OrganizationAssessMapper;
import com.recSys.model.UserOrgBehavior;
import com.recSys.util.FileHandler;
import net.librec.common.LibrecException;
import net.librec.conf.Configuration;
import net.librec.data.model.TextDataModel;
import net.librec.recommender.Recommender;
import net.librec.recommender.RecommenderContext;
import net.librec.recommender.cf.ItemKNNRecommender;
import net.librec.recommender.item.RecommendedItem;
import net.librec.similarity.PCCSimilarity;
import net.librec.similarity.RecommenderSimilarity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.recSys.util.LogRecord.INFO;

/**
 * Created by dell on 2017/8/8.
 */
@Component
public class ScheduledDataCollection {

    @Autowired
    OrganizationAssessMapper organizationAssessMapper;
    @Autowired
    CollectionMapper collectionMapper;

    /**
     * 服务于推荐殡仪馆
     * @throws LibrecException
     */
    @Scheduled(cron = "0 0 1,7,13,19 * * *")
    public void schedule() throws LibrecException {
        INFO.info("start to execute offline task");
        //提取用户机构行为数据并写入文件
        List<UserOrgBehavior> uobList = new ArrayList<>();
        List<UserOrgBehavior> ratingList = organizationAssessMapper.selectAll();
        uobList.addAll(ratingList);
        List<UserOrgBehavior> collectList = collectionMapper.selectUserOrgRecord();
        for (UserOrgBehavior uob: collectList
                ) {
            if(uobList.contains(uob)) {
                uobList.get(uobList.indexOf(uob)).setCollect(1);
            } else {
                uob.setCollect(1);
                uobList.add(uob);
            }
        }
        new FileHandler().saveUserOrgRating(uobList);

        //用itemKNN计算出给每个用户推荐的机构
        Configuration conf = new Configuration();
        conf.set("dfs.data.dir", "D:/git/101RecSys/offlineTaskOutput");
        conf.set("data.input.path","userOrgRating.txt");
        TextDataModel dataModel = new TextDataModel(conf);
        dataModel.buildDataModel();
        RecommenderContext context = new RecommenderContext(conf, dataModel);
        conf.set("rec.recommender.similarity.key" ,"item");
        conf.set("rec.recommender.similarity.shrinkage" ,"10");
        RecommenderSimilarity similarity = new PCCSimilarity();
        similarity.buildSimilarityMatrix(dataModel);
        context.setSimilarity(similarity);
        conf.set("rec.neighbors.knn.number", "50");
        conf.set("rec.recommender.isranking", "true");
        conf.set("rec.recommender.ranking.topn", "10");
        Recommender recommender = new ItemKNNRecommender();
        recommender.setContext(context);
        recommender.recommend(context);
        List<RecommendedItem> recommendedItemList = recommender.getRecommendedList();
        new FileHandler().saveRecommendOrgListByItemKNN(recommendedItemList);
    }


}
